<template>
  <div class="app-container">
    <div class="yz-header-banner-wrapper">
      <div class="banner">
        <h1>考研初试资源查询</h1>
        <div class="ch-sub-title">用户可选择各类条件查询当年硕士招生专业的考试资源</div>
      </div>
    </div>
    <div class="search-container">
      <el-form :model="queryParams" ref="queryForm" :rules="rules"
               size="small" :inline="true" v-show="showSearch" label-width="150px">
        <el-row :gutter="40"> <!-- 这里可以调整列间距 -->
          <el-col :span="10">
            <el-form-item label="省份" prop="provinceId" size="50px">
              <el-select v-model="queryParams.provinceId"
                         style="width: 100%"
                         placeholder="请选择省份"
                         @change="checkProvincePromotion(queryParams.provinceId)"
                         clearable>
                <el-option v-for="item in provinceList"
                           :key="item.id"
                           :label="item.name"
                           :value="item.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="高校名称" prop="universityId" size="50px">
              <el-select
                v-model="value"
                filterable
                remote
                reserve-keyword
                placeholder="请输入高校名称"
                :remote-method="remoteMethod"
                clearable
                @change="handleUniversityChange(value)"
                :loading="universityloading">
                <el-option
                  v-for="item in universityList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="40"> <!-- 这里可以调整列间距 -->
          <el-col :span="10">
            <el-form-item label="门类名称" prop="categoryId" size="50px">
              <el-select v-model="queryParams.categoryId"
                         style="width: 100%"
                         placeholder="请选择门类"
                         clearable
                         @change="checkCategoryPromotion(queryParams.categoryId)">
                <el-option v-for="item in categoryList"
                           :key="item.id"
                           :label="item.name"
                           :value="item.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="学科名称" prop="subjectId" size="50px">
              <el-select v-model="queryParams.subjectId"
                         style="width: 100%"
                         placeholder="请选择学科"
                         clearable
                         @change="checkSubjectPromotion(queryParams.subjectId)">
                <el-option v-for="item in subjectList"
                           :key="item.id"
                           :label="item.name"
                           :value="item.id"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="40"> <!-- 这里可以调整列间距 -->
          <el-col :span="10">
            <el-form-item label="专业名称" prop="majorId" size="50px">
              <el-select v-model="queryParams.majorId"
                         style="width: 100%"
                         placeholder="请选择专业"
                         clearable
                         @change="checkMajorPromotion(queryParams.majorId)">
                <el-option v-for="item in majorList"
                           :key="item.id"
                           :label="item.name"
                           :value="item.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="考试科目" prop="courseId" size="50px">
              <el-select v-model="queryParams.courseId"
                         style="width: 100%"
                         placeholder="请选择考试科目"
                         clearable>
                <el-option v-for="item in courseList"
                           :key="item.id"
                           :label="item.name"
                           :value="item.id"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="medium" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="medium" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <el-table v-loading="loading" :data="searchList"
              @selection-change="handleSelectionChange" v-show="searchList.length > 0">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="高校" align="center" prop="universityName"/>
      <el-table-column label="专业" align="center" prop="majorName"/>
      <el-table-column label="考试科目" align="center" prop="courseName"/>
      <el-table-column label="资源名称" align="center" prop="resourceName"/>
      <el-table-column label="资源类型" align="center" prop="classification">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.resource_classification" :value="scope.row.classification"/>
        </template>
      </el-table-column>
      <el-table-column label="资源内容" align="center" prop="content"/>
      <el-table-column label="资源描述" align="center" prop="description"/>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listSearch, getProvinceList, getCategoryList, getSubjectList, getMajorList, getCourseList, getUniversityList
} from "@/api/search/search";


export default {
  name: "Search",
  dicts: ['resource_classification'],
  data() {
    return {
      // 遮罩层
      loading: false,
      universityloading: false,
      //学校id
      universityId: "",
      //学校列表
      universityList: [],
      value: [],
      list: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      //省份id
      provinceId: "",
      //省份列表
      provinceList: [],
      //门类id
      categoryId: "",
      //门类列表
      categoryList: [],
      //学科id
      subjectId: "",
      //学科列表
      subjectList: [],
      //专业id
      majorId: "",
      //专业列表
      majorList: [],
      //考试科目id
      courseId: "",
      //考试科目列表
      courseList: [],
      // 总条数
      total: 0,
      // 资源查询表格数据
      searchList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        provinceId: null,
        universityId: null,
        categoryId: null,
        subjectId: null,
        majorId: null,
        courseId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        universityId: [
          {required: true, message: "学校名称不能为空", trigger: "blur"}
        ],
        categoryId: [
          {required: true, message: "门类名称不能为空", trigger: "blur"}
        ],
        subjectId: [
          {required: true, message: "学科名称不能为空", trigger: "blur"}
        ],
        majorId: [
          {required: true, message: "专业名称不能为空", trigger: "blur"}
        ],

      }
    };
  },
  mounted() {
    this.getProvinceList();
    this.getUniversityList();
  },
  created() {
    //this.getList();
  },
  methods: {
    /** 查询资源查询列表 */
    getList() {
      this.$refs["queryForm"].validate(valid => {
        if (valid) {
          this.loading = true;

          listSearch(this.queryParams).then(response => {
            this.searchList = response.rows;
            this.total = response.total;
            this.loading = false;
          });
        }
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.universityList = [];
      // 清空输入框
      this.value = '';
      this.queryParams.universityId = null;
      this.categoryList = [];
      this.subjectList = [];
      this.majorList = [];
      this.searchList = [];
      this.courseList = [];
      this.resetForm("queryForm");
      this.total = 0;
      //获取所有学校列表
      this.getUniversityList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    //检查省份下拉框的选择
    checkProvincePromotion(provinceId) {
      this.getUniversityList(provinceId);
      this.value = null;
      this.queryParams.universityId = null;
      this.queryParams.categoryId = null;
      this.queryParams.subjectId = null;
      this.queryParams.majorId = null;
      this.queryParams.courseId = null;
      this.subjectList = [];
      this.majorList = [];
      this.courseList = [];
      //this.getCategoryList();//重新获取门类列表和学科列表
      //this.getSubjectList(-1);
    },
    //获取省份下拉框
    getProvinceList() {
      //this.loading = true;
      getProvinceList()
        .then(res => {
          this.provinceList = res.data;
        }).catch(() => {
        // 处理错误
      });
    },
    //获取门类下拉框
    getCategoryList() {
      let universityId = this.queryParams.universityId !== null ? this.queryParams.universityId : -1;
      //this.loading = true;
      getCategoryList(universityId).then(res => {
          this.categoryList = res.data;
        }).catch(() => {
        // 处理错误
      });
    },
    checkCategoryPromotion(categoryId) {
      this.queryParams.subjectId = null;
      this.queryParams.majorId = null;
      this.queryParams.courseId = null;
      this.subjectList = [];
      this.majorList = [];
      this.courseList = [];
      //门类清空操作
      if (this.queryParams.categoryId !== '' && this.queryParams.categoryId !== null) {
        this.getSubjectList(categoryId); // 获取学科列表
      }
    },
    //获取学科下拉框
    getSubjectList(categoryId) {
      this.queryParams.subjectId = null;
      getSubjectList(categoryId).then(res => {
        this.subjectList = res.data;
      }).catch(() => {
        // 处理错误
      });
    },
    checkSubjectPromotion(subjectId) {
      if (this.queryParams.subjectId !== '') this.getMajorList(subjectId);
      //每次选择门类都将已选择的学科清空
      this.subjectId = null;
      this.queryParams.majorId = null;
      this.majorList = [];
      this.queryParams.courseId = null;
      this.courseList = [];
    },
    //获取学科下拉框
    getMajorList(subjectId) {
      this.queryParams.majorId = null;
      subjectId = this.queryParams.subjectId !== null ? this.queryParams.subjectId : -1;
      getMajorList(subjectId).then(res => {
        this.majorList = res.data;
      }).catch(() => {
        // 处理错误
      });
    },
    checkMajorPromotion(majorId) {
      if (this.queryParams.majorId !== '') this.getCourseList(majorId);
      //每次选择门类都将已选择的学科清空
      this.majorId = null;
      this.queryParams.courseId = null;
      this.courseList = [];
    },
    //获取学科下拉框
    getCourseList(majorId) {
      this.queryParams.courseId = null;
      getCourseList(majorId).then(res => {
        this.courseList = res.data;
      }).catch(() => {
        // 处理错误
      });
    },
    //获取学校联想框
    getUniversityList() {
      this.universityloading = true;
      let provinceId = this.queryParams.provinceId || -1; // 默认值为-1或选定的省份ID
      getUniversityList(provinceId)
        .then(res => {
          this.list = res.data.map(item => {
            return {value: `${item.id}`, label: `${item.name}`};
          });
          this.universityloading = false;
        }).catch(() => {
        // 处理错误
        this.universityloading = false;
      });

    },
    remoteMethod(query) {
      if (query !== '') {
        this.universityloading = true;
        setTimeout(() => {
          this.universityloading = false;
          this.universityList = this.list.filter(item => {
            return item.label.toLowerCase().indexOf(query.toLowerCase()) > -1;
          });

        }, 200);
      } else {
        this.universityList = [];
        // 清空输入框
        this.value = '';
        this.queryParams.universityId = null; // 清空学校名称
      }
    },
    // 处理选择学校名称变化
    handleUniversityChange() {
      if (this.value || this.value === 0) { // 添加对0值的判断
        this.queryParams.universityId = this.value;
        //选择新的学校名称后，选择新的列表
        this.getCategoryList();
      } else {
        this.queryParams.universityId = null;
      }
      this.queryParams.categoryId = null;
      this.queryParams.subjectId = null;
      this.queryParams.majorId = null;
      this.queryParams.courseId = null;
      this.categoryList = [];
      this.subjectList = [];
      this.majorList = [];
      this.courseList = [];
    },
  }
};
</script>
<style>
.yz-header-banner-wrapper {
  margin-bottom: 15px;
  background: linear-gradient(180deg, #288ddb 5%, #3091db 73%);
}

.yz-header-banner-wrapper .banner {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  height: 148px;
  line-height: 128px;
  background: url("https://t2.chei.com.cn/yz/assets/images/header-banner.png") no-repeat center top;
  color: #fff;
  text-shadow: 0 2px 2px rgb(0 0 0 / 6%);
}

.yz-header-banner-wrapper h1 {
  line-height: 20px;
  font-size: 38px;
  font-weight: 700;
}

.yz-header-banner-wrapper .ch-sub-title {
  margin-top: 0px;
  line-height: 26px;
  font-size: 16px;
}

.search-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  text-align: center;
  height: 40vh; /* 可以根据需要调整垂直居中 */
  padding: 100px;
}

</style>
